################################################################################
# Title   : Replication Code — Union Spillovers Paper
# Project : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
# Authors : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
# Output  : Figures 6
# Date    : August 2025
################################################################################

# Set default graphical theme
theme_set(theme_light())

# Create output directory if it doesn't exist
if (!dir.exists(figs)) dir.create(figs, recursive = TRUE)

# Load data
dat <- read_dta(paste0(wd,"/decomp_grp_all_case_6.dta")) %>% 
  mutate(year = case_when(
    year2 ==  1 ~ "1980",
    year2 ==  2 ~ "1990",
    year2 ==  3 ~ "2000",
    year2 ==  4 ~ "2010")) 

dat <- dat %>% 
  # select(year, observed, cf1, cf2, cf3  ) %>% 
  mutate(year = as.Date(ISOdate(year, 1, 1)))

data <- dat

# Transform data into long format
data_long <- data %>%
  mutate(cf_barg = cf_transition + cf_premia) %>% 
  pivot_longer(
    cols = c(deunion, cf_threat, cf_premia, cf_transition, cf_barg), 
    names_to = "Category",        
    values_to = "Value"           
  ) %>% 
  mutate(Category = fct_relevel(Category, 
                                "deunion",        # Non-Union Spillover
                                "cf_threat",      # Union Threat Effect (4a)
                                "cf_barg",        # Bargaining Effect (4b)
                                "cf_premia",      # Union Wage Premia Effect (4b.1)
                                "cf_transition")) # Transitions Effect (4b.2)

data_long <- data_long %>% 
  filter(! Category %in% c("cf_premia","cf_transition")) %>%
  mutate(across(where(is.factor), fct_drop))

# Plot the data
ggplot(data_long, aes(x = year, 
                      y = Value, 
                      color = Category, 
                      shape = Category, 
                      linetype = Category)) +
  geom_point(size=3) +
  geom_line(size=1) +
  labs(
    y = "Change in Log Wage",
    x = "Year"
  ) +
  scale_color_discrete(name = "Category", 
                       labels = c("Non-Union Spillover",
                                  "Union Threat Effect (4a) ",
                                  "Bargaining Effect (4b)"
                                  #   "Union Wage Premia Effect (4b.1) ",
                                  #   "Transitions Effect (4b.2)"
                       ))+ 
  scale_linetype_discrete(name = "Category",
                          labels = c("Non-Union Spillover",
                                     "Union Threat Effect (4a) ",
                                     "Bargaining Effect (4b)"
                                     #   "Union Wage Premia Effect (4b.1) ",
                                     #   "Transitions Effect (4b.2)"
                          ))+  
  scale_shape_manual(values = c("deunion" = 16, "cf_threat" = 17, "cf_barg" = 19 ), # "cf_transition" = 1 "cf_premia" = 2, ),
                     name = "Category", 
                     labels = c("Non-Union Spillover",
                                "Union Threat Effect (4a) ",
                                "Bargaining Effect (4b)"
                                #  "Union Wage Premia Effect (4b.1) ",
                                #  "Transitions Effect (4b.2)"
                     ))+
  scale_x_date(
    expand = c(0.01, 0.01), 
    date_labels = "%Y"
  ) +
  theme_minimal() +
  theme(
    legend.position = c(0, 0),  # Bottom-left corner
    legend.justification = c(0, 0),  # Align bottom-left
    legend.background = element_rect(fill = "white", color = "black"),  # Add background to legend
    legend.title = element_blank(),  # Remove legend title
    legend.text = element_text(size = 13),  # Adjust legend text size
    legend.key.width = unit(2, "lines"),
    legend.key.height = unit(1, "lines")
  ) +
  
  # Adjust the guides to have 2 rows and 2 columns
  guides(
    linetype = guide_legend(nrow = 3, ncol = 1, byrow = TRUE),
    colour = guide_legend(nrow = 3, ncol = 1, byrow = TRUE),
    shape = guide_legend(nrow = 3, ncol = , byrow = TRUE)
  )  -> p2

p2

ggsave(paste0(figs,"/Figure_6.pdf"),
       units = c("in"), width = 9, height = 4.5)
